package com.kws.tool.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kws.tool.dto.QueryPolicyMaintenanceRq;
import com.kws.tool.entity.PolicyEntity;
import com.kws.tool.vo.PolicyVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author : zhangbenting
 * @date : 2024/10/22 10:50
 * @Version: 1.0
 */
public interface PolicyMapper extends BaseMapper<PolicyEntity> {

    @Select("<script>" +
            "SELECT " +
            " a.*, " +
            " b.collect_id  " +
            " FROM " +
            " `tb_tool_policy` AS a " +
            " LEFT JOIN ( SELECT collect_id, collect_type_id FROM `tb_tool_collect`  " +
            " WHERE collect_type = 0 AND user_id = ${userId} ) AS b ON a.id = b.collect_type_id " +
            " WHERE 1=1 " +
            " <if test='policyName!=null and policyName!=\"\"'>  AND a.policy_name like '%${policyName}%'  </if>  " +
            " <if test='level!=null and level!=\"\"'>  AND a.level = '${level}'  </if>  " +
            " <if test='economize!=null and economize!=\"\"'>  AND a.economize = '${economize}'  </if>  " +
            " <if test='market!=null and market!=\"\"'>  AND a.market = '${market}'  </if>  " +
            " <if test='district!=null and district!=\"\"'>  AND a.district = '${district}'  </if>  " +
            " <if test='organ!=null and organ!=\"\"'>  AND a.organ in (${organ}) </if>  " +
            " <if test='startTime!=null and startTime!=\"\" and endTime!=null and endTime!=\"\"'>  AND a.create_time  BETWEEN '${startTime}' and '${endTime}' </if>  " +
            " LIMIT ${ page } , ${ size}" +
            "</script>"
    )
    List<PolicyVo> getPolicyList(@Param("page") Integer page,
                                 @Param("size") Integer size,
                                 @Param("policyName") String policyName,
                                 @Param("level") String level,
                                 @Param("economize") String economize,
                                 @Param("market") String market,
                                 @Param("district") String district,
                                 @Param("organ") String organ,
                                 @Param("userId") Long userId,
                                 @Param("startTime") String startTime,
                                 @Param("endTime") String endTime);

    @Select("<script>" +
            "SELECT " +
            " a.*, " +
            " b.collect_id  " +
            " FROM " +
            " (SELECT collect_id, collect_type_id FROM `tb_tool_collect` WHERE " +
            " collect_type = 0 AND user_id = ${userId} ) AS b " +
            " LEFT JOIN   " +
            " tb_tool_policy AS a ON a.id = b.collect_type_id " +
            " WHERE 1=1 " +
            " <if test='policyName!=null and policyName!=\"\"'>  AND a.policy_name like '%${policyName}%'  </if>  " +
            " <if test='level!=null and level!=\"\"'>  AND a.level = '${level}'  </if>  " +
            " <if test='economize!=null and economize!=\"\"'>  AND a.economize = '${economize}'  </if>  " +
            " <if test='market!=null and market!=\"\"'>  AND a.market = '${market}'  </if>  " +
            " <if test='district!=null and district!=\"\"'>  AND a.district = '${district}'  </if>  " +
            " <if test='organ!=null and organ!=\"\"'>  AND a.organ in (${organ}) </if>  " +
            " <if test='startTime!=null and startTime!=\"\" and endTime!=null and endTime!=\"\"'>  AND a.create_time  BETWEEN '${startTime}' and '${endTime}' </if>  " +
            " LIMIT ${ page } , ${ size}" +
            "</script>"
    )
    List<PolicyVo> getUserCollectPolicyList(@Param("page") Integer page,
                                            @Param("size") Integer size,
                                            @Param("policyName") String policyName,
                                            @Param("level") String level,
                                            @Param("economize") String economize,
                                            @Param("market") String market,
                                            @Param("district") String district,
                                            @Param("organ") String organ,
                                            @Param("userId") Long userId,
                                            @Param("startTime") String startTime,
                                            @Param("endTime") String endTime);

    Page<PolicyEntity> queryPolicyMaintenancePage(@Param("page") Page<PolicyEntity> page, @Param("query") QueryPolicyMaintenanceRq query);
}
